﻿using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Styling;
using Avalonia.Themes.Fluent;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace SchoolApp.ViewModels;

public partial class MainViewModel : ViewModelBase
{
    // 通过构造函数注入日志服务
    private readonly ILogger<MainViewModel> _logger;
    private IFreeSql _fsql;

    public MainViewModel()
    {
        LoginCode = "admin";
    }

    public MainViewModel(ILogger<MainViewModel> logger, IFreeSql fsql)
    {
        _logger = logger;
        _fsql = fsql;
        _logger.LogInformation("初始化MainWindowViewModel");

        LoginCode = Common.LoginOperator.Code;
        ExecuteTurnFormCmd();
    }

    #region 属性

    private string _pageTitle;

    public string PageTitle
    {
        get => _pageTitle;
        set => SetProperty(ref _pageTitle, value);
    }

    private string _loginCode;

    public string LoginCode
    {
        get => _loginCode;
        set => SetProperty(ref _loginCode, value);
    }

    private string _themeIcon = "mdi-theme-light-dark";

    public string ThemeIcon
    {
        get => _themeIcon;
        set => SetProperty(ref _themeIcon, value);
    }

    private string _themeName = "跟随系统";

    public string ThemeName
    {
        get => _themeName;
        set => SetProperty(ref _themeName, value);
    }

    private ViewModelBase _viewModelModel;

    public ViewModelBase ViewModel
    {
        get => _viewModelModel;
        set => SetProperty(ref _viewModelModel, value);
    }

    #endregion

    #region 命令
    
    #region 切换主题

    private RelayCommand _changeThemeCmd;

    public RelayCommand ChangeThemeCmd =>
        _changeThemeCmd ??= new RelayCommand(ExecuteChangeThemeCmd);

    private void ExecuteChangeThemeCmd()
    {
        // 获取当前 FluentTheme 实例
        var fluentTheme = Application.Current.Styles.OfType<FluentTheme>().FirstOrDefault();
        if (fluentTheme == null)
            return;

        if (Application.Current.RequestedThemeVariant == ThemeVariant.Default)
        {
            // 切换到 Light 模式
            Application.Current.RequestedThemeVariant = ThemeVariant.Light;
            ThemeIcon = "mdi-white-balance-sunny";
            ThemeName = "亮色主题";
        }
        else if (Application.Current.RequestedThemeVariant == ThemeVariant.Light)
        {
            // 切换到 Dark 模式
            Application.Current.RequestedThemeVariant = ThemeVariant.Dark;
            ThemeIcon = "mdi-weather-night";
            ThemeName = "暗色主题";
        }
        else
        {
            // 切换到 Default 模式
            Application.Current.RequestedThemeVariant = ThemeVariant.Default;
            ThemeIcon = "mdi-theme-light-dark";
            ThemeName = "跟随系统";
        }
    }
    
    #endregion

    #region 切换到班级

    private RelayCommand _turnFormCmd;

    public RelayCommand TurnFormCmd =>
        _turnFormCmd ??= new RelayCommand(ExecuteTurnFormCmd);

    private void ExecuteTurnFormCmd()
    {
        if (PageTitle == "班级管理")
            return;
        PageTitle = "班级管理";
        ViewModel = Program.ServiceProvider.GetRequiredService<FormMgrViewModel>();
    }
    
    #endregion

    #region 切换到老师

    private RelayCommand _trunTeacherCmd;

    public RelayCommand TurnTeacherCmd =>
        _trunTeacherCmd ??= new RelayCommand(ExecuteTurnTeacherCmd);

    private void ExecuteTurnTeacherCmd()
    {
        if (PageTitle == "老师管理")
            return;
        PageTitle = "老师管理";
        ViewModel = Program.ServiceProvider.GetRequiredService<TeacherMgrViewModel>();
    }

    #endregion

    #region 切换到学生

    private RelayCommand _turnStudentCmd;

    public RelayCommand TurnStudentCmd =>
        _turnStudentCmd ??= new RelayCommand(ExecuteTurnStudentCmd);

    private void ExecuteTurnStudentCmd()
    {
        if(PageTitle == "学生管理")
            return;
        
        PageTitle = "学生管理";
        ViewModel = Program.ServiceProvider.GetRequiredService<StudentMgrViewModel>();
    }

    #endregion

    #endregion
}